package com.kaiyidiannao.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import com.kaiyidiannao.common.BaseActionBarActivity;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.main.R;
import com.kaiyidiannao.model.bean.PlayBean;
import com.kaiyidiannao.model.req.model.PostDeleteMessageInfo;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by mark on 14-4-11.
 */
public class FoodPalaceAdapter extends ImageLoaderBaseAdapter {
    private ImageLoader imageLoader;
    private LayoutInflater layoutInflater;
    private List<PlayBean> list = new ArrayList<PlayBean>();
    //
    private int typeid = DataCenter.typeid;
    private String userid;
    private Context context;
    private int firstIn = 0;

    public FoodPalaceAdapter(Context context, List<PlayBean> list) {
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));
        try {
            this.list = list;
        } catch (IndexOutOfBoundsException e) {

        }
        this.layoutInflater = LayoutInflater.from(context);
        this.context = context;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        try {
            return list.get(position);
        } catch (IndexOutOfBoundsException e) {
            return 0;
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ImageView imageView = null;
        TextView nameTextView = null;
        TextView addrTextView = null;
        TextView foodPalaceDistance = null;
        RatingBar rating = null;
        RatingBar rating1 = null;
        ImageButton foodPalaceLocation = null;
        ImageButton btDelete;
        PlayBean bean = null;

        try {
            bean = list.get(position);
            if (convertView == null) {
                convertView = layoutInflater.inflate(R.layout.foodpalace_item, null);
            }
            rating1 = (RatingBar) convertView.findViewById(R.id.rating1);
            btDelete = (ImageButton) convertView.findViewById(R.id.btDelete);
            foodPalaceLocation = (ImageButton) convertView.findViewById(R.id.foodPalaceLocation);
            foodPalaceDistance = (TextView) convertView.findViewById(R.id.foodPalaceDistance);
            imageView = (ImageView) convertView.findViewById(R.id.foodPic);
            nameTextView = (TextView) convertView.findViewById(R.id.storeName);
            addrTextView = (TextView) convertView.findViewById(R.id.storeAddress);
            rating = (RatingBar) convertView.findViewById(R.id.rating);
//            if(bean.getThumb() == null){
//                imageView.setImageBitmap(BitmapFactory.decodeResource(context.getResources(),R.drawable.icon_default));
//            }else {
            imageLoader.displayImage(bean.getThumb(), imageView, options);
//            }
            if (null == bean.getAddress() || bean.getAddress().equals("")) {
                addrTextView.setVisibility(View.GONE);
                foodPalaceLocation.setVisibility(View.GONE);
            } else {
                addrTextView.setText(bean.getAddress());
            }
            if (null == bean.getDistance() || bean.getDistance().equals("")) {
                foodPalaceDistance.setVisibility(View.GONE);
            } else {
                float b = (float) (Math.round((Float.parseFloat(bean.getDistance()) / 1000) * 100)) / 100;
                foodPalaceDistance.setText(b + "km");
            }
            if (null == bean.getStar() || bean.getStar().equals("")) {
                rating.setVisibility(View.GONE);
                rating1.setVisibility(View.GONE);
            } else {
                rating1.setRating(5);
                //rating.setNumStars(Integer.parseInt(bean.getStar()));
                rating.setRating(Integer.parseInt(bean.getStar()));
            }
            nameTextView.setText(bean.getTitle());
            if (DataCenter.userLogin.size() != 0 && DataCenter.userLogin != null) {
                userid = DataCenter.userLogin.get(0).getUserid();
                final PlayBean finalBean = bean;
                btDelete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (typeid == 16 || typeid == 32) {
                            String action = "delete";
                            DataCenter.position = position;
                            Log.d("FoodPalaceAdapter ", finalBean.getFid() + "  " + position + "");
                            ((BaseActionBarActivity) context).showProgressDialog();
                            new PostDeleteMessageInfo(finalBean.getFid(), userid, action).execute();
                        }
                    }
                });
            }
        } catch (IndexOutOfBoundsException e) {

        }
        return convertView;
    }
}
